//
//  YZNumView.m
//  VET
//
//  Created by Lam BG on 2018/7/25.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZNumView.h"

#import "YZWBAttentionView.h"
#import "YZNumCircle.h"
#import "UICountingLabel.h"

#import "UIView+Frame.h"

@interface YZNumView ()

@property (weak, nonatomic) IBOutlet UICountingLabel *numLabel;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@property (weak, nonatomic) YZNumCircle *numCircle;


@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
@property (weak, nonatomic) IBOutlet UILabel *subTipsLabel;

@property (weak, nonatomic) IBOutlet UIView *attentionView;
@property (weak, nonatomic) YZWBAttentionView *wbAttentionView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *attentionConst;

@end

@implementation YZNumView

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    self.numLabel.format = @"%d";
    self.numLabel.method = UILabelCountingMethodLinear;
    
    if (kIsiPhone6) {
        
        self.numLabel.font  = [UIFont systemFontOfSize:38];
        self.textLabel.font = [UIFont systemFontOfSize:16];
        self.attentionConst.constant = -12;
        
    } else if (kIsiPhoneSmall) {
        
        self.numLabel.font  = [UIFont systemFontOfSize:32];
        self.textLabel.font = [UIFont systemFontOfSize:12];
        self.attentionConst.constant = -8;
    }
    
    self.attentionView.layer.cornerRadius = self.attentionView.frame.size.height / 2;

    // 画圆 （UI 改变先隐藏）
//    float size = self.width;
//    YZNumCircle *view = [[YZNumCircle alloc] initWithFrame:CGRectMake(0, 0, size, size) lineWidth:6];
//    [self insertSubview:view atIndex:0];
//    _numCircle = view;
}

#pragma mark - public function

- (void)updateNum:(NSInteger)num {
    
    NSInteger currentNum = [self.numLabel.text integerValue];
    if (currentNum != num) {
        
        [self.numLabel countFrom:0 to:num withDuration:.5];
    }
    
    // 待办
    if (self.nvType == NumViewTypeDaily) {
        if (num >= 5) {
            
            // 当未完成任务数大于等于5时，显示红色
            [self.wbAttentionView changeStyle:(AttentionViewStyleWorkWarnning)];
        } else if (num >= 1) {
            
            // 当未完成任务数大于0小于5时，显示黄色
            [self.wbAttentionView changeStyle:(AttentionViewStyleWorkTip)];
        } else {
            
            // 当未完成任务数等于0时，显示系统默认色
            [self.wbAttentionView changeStyle:(AttentionViewStyleWorkNormal)];
        }
    }
    
    // 笔记
    if (self.nvType == NumViewTypeNote) {
        
        if (num >= 1) {
            
            // 笔记 有需要处理时
            [self.wbAttentionView changeStyle:(AttentionViewStyleNoteEdit)];
        } else {
            
            // 没有笔记
            [self.wbAttentionView changeStyle:(AttentionViewStyleNoteNormal)];
        }
    }
}

- (void)changeColorType:(NumViewColorType)colorType {
    
    switch (colorType) {
            
        case NumViewColorTypeTip:
        {
            self.numLabel.textColor  = kColorWithHex(0xFBA84E);
            self.textLabel.textColor = kColorWithHex(0xFBA84E);
        }
            break;
            
        case NumViewColorTypeWarning:
        {
            self.numLabel.textColor  = kColorWithHex(0xFF5656);
            self.textLabel.textColor = kColorWithHex(0xFF5656);
        }
            break;
            
        default:
        {
            self.numLabel.textColor  = kColorWithHex(0x151515);
            self.textLabel.textColor = kColorWithHex(0x151515);
        }
            break;
    }
    
    // 圆变色（UI 改变先隐藏）
//    [self.numCircle changeColorType:colorType];
}

#pragma mark - setter

- (void)setNvType:(NumViewType)nvType {

    _nvType = nvType;
    
    switch (nvType) {
            
        case NumViewTypeDaily: // 待办
        {
            self.textLabel.text = @"项";
            self.tipsLabel.text = @"每日待办";
            self.subTipsLabel.text = @"今日未完成任务项";
        }
            break;
            
        case NumViewTypeNote: // 笔记
        {
            self.textLabel.text = @"篇";
            self.tipsLabel.text = @"工作笔记";
            self.subTipsLabel.text = @"近三天记录笔记数";
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - getter

- (YZWBAttentionView *)wbAttentionView {
    
    if (_wbAttentionView == nil) {
        
        YZWBAttentionView *attView = [[NSBundle mainBundle] loadNibNamed:@"YZWBAttentionView" owner:self options:nil].firstObject;
        attView.frame = self.attentionView.frame;
        attView.origin = CGPointMake(0, 0);
        
        [self.attentionView addSubview:attView];
        _wbAttentionView = attView;
    }
    
    return _wbAttentionView;
}

@end















